package com.net.rip.utils;

import com.net.rip.entity.Information;
import com.net.rip.entity.Network;
import com.net.rip.entity.Router;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class netUpdate {

    /**
     * 网络加入
     *
     * @param network
     */
    public static void netAdd(Network network) {

        System.out.println("请输入要加入的网络名称：");
        String net;
        Scanner in = new Scanner(System.in);
        net = in.nextLine();

        System.out.println("请输入与该网络相邻的路由器名称（空格分隔）：");
        String str = in.nextLine();
        String routers[] = str.split(" ");

        for (int i = 0; i < routers.length; i++) {
            for (int j = 0; j < network.getRouterList().size(); j++) {
                Router router = network.getRouterList().get(j);
                if (router.getRouterName().equals(routers[i])) {
                    router.getInformation().put(net, new Information(net, 1, "null"));
                }
            }
        }

        for (int i = 0; i < network.getRouterNum(); i++) {
            System.out.println(network.getRouterList().get(i));
        }

    }

    /**
     * 网络退出
     *
     * @param network
     */
    public static void netDel(Network network) {

        System.out.println("请输入要退出的网络名称：");
        String net;
        Scanner in = new Scanner(System.in);
        net = in.nextLine();

        for (int i = 0; i < network.getRouterList().size(); i++) {
            Router router = network.getRouterList().get(i);
            if (router.getInformation().containsKey(net)) {
                router.getInformation().remove(net);
            }
        }

        for (int i = 0; i < network.getRouterNum(); i++) {
            System.out.println(network.getRouterList().get(i));
        }

    }

    /**
     * 路由器故障
     *
     * @param network
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static void netTrouble(Network network) throws ClassNotFoundException, IOException {
        System.out.println("请输入发生故障的路由器名称：");
        String troubleRouter;
        Scanner in = new Scanner(System.in);
        troubleRouter = in.nextLine();
        //1、删除troubleRouter所有邻接路由相关的路由信息
        Iterator<Router> routerList = network.getRouterList().iterator();
        while (routerList.hasNext()) {
            Router router = (Router) routerList.next();
            if (router.getRouterName().equals(troubleRouter)) {
                //得到其相邻路由器
                List<Router> nearRouterList = router.getNearRouter();
                for (int i = 0; i < nearRouterList.size(); i++) {
                    Router nearRouter = nearRouterList.get(i);

                    //从相邻路由器的相邻路由表中删除
                    for (int j = 0; j < nearRouter.getNearRouter().size(); j++) {
                        if (nearRouter.getNearRouter().get(j).getRouterName().equals(troubleRouter)) {
                            nearRouter.getNearRouter().remove(j);
                        }
                    }
                }
                //清空Map集合
                router.getInformation().clear();
                //将此路由器从List集合删除
                routerList.remove();
                network.setRouterNum(network.getRouterNum() - 1);
            }
        }
        //2、删除下一跳为troubleRouter的表项
        Iterator<Router> it = network.getRouterList().iterator();
        while (it.hasNext()) {
            Router router = (Router) it.next();
            Iterator<String> its = router.getInformation().keySet().iterator();
            while (its.hasNext()) {
                String str = (String) its.next();
                if (router.getInformation().get(str).getNextJumpRouter().equals(troubleRouter)) {
                    its.remove();
                }
            }
        }

        for (int i = 0; i < network.getRouterList().size(); i++) {
            System.out.println(network.getRouterList().get(i));
        }

    }

    /**
     * 更改网络拓扑
     * 循环
     * @param network
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static void doCycle(Network network) throws ClassNotFoundException, IOException {

        int choose; //输入的选项
        Scanner scanner = new Scanner(System.in);

        System.out.println("********更改拓扑********");
        System.out.println("1.网络加入");
        System.out.println("2.网络退出");
        System.out.println("3.路由器故障");
        System.out.println("4.取消");
        System.out.println("***********************");

        System.out.println("请选择：");
        choose = scanner.nextInt();

        switch (choose) {
            case 1:
                netAdd(network);
                break;
            case 2:
                netDel(network);
                break;
            case 3:
                netTrouble(network);
                break;
            case 4:
                break;
        }
    }
}
